package com.hqyj.ssm_project.service;


import com.hqyj.ssm_project.mapper.AttendanceDateMapper;
import com.hqyj.ssm_project.mapper.AttendanceTimeMapper;
import com.hqyj.ssm_project.mapper.CardMapper;

import com.hqyj.ssm_project.pojo.AttendanceDate;
import com.hqyj.ssm_project.pojo.AttendanceStatis;
import com.hqyj.ssm_project.pojo.AttendanceTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AttendanceServiceImp implements AttendanceService{

    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private AttendanceDateMapper dateMapper;
    @Autowired
    private AttendanceTimeMapper timeMapper;


    @Override
    public void insertDateAndTime(Date date, Integer userId) throws ParseException {

//        //查询是否存在今天的用户数据
//       SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//       String datetime=sDateFormat.format(date);
//        String time = datetime.substring(0, 10);
//        Date parse = sDateFormat.parse(time);
        AttendanceDate attendanceDate = new AttendanceDate();
        attendanceDate.setUserId(userId);
        attendanceDate.setBrushDate(date);
        AttendanceDate flag= dateMapper.selectExistUser(attendanceDate);
        System.out.println(flag);

        AttendanceTime attendanceTime = new AttendanceTime();
        if(flag==null){
            //不存在
            dateMapper.insert(attendanceDate);
            System.out.println("自增id"+attendanceDate.getAttendanceId());
            attendanceTime.setAttendanceId(attendanceDate.getAttendanceId());
            attendanceTime.setBrushTime(date);
            timeMapper.insert(attendanceTime);
        }else {
            //存在
            attendanceTime.setAttendanceId(flag.getAttendanceId());
            attendanceTime.setBrushTime(date);
            timeMapper.insert(attendanceTime);
        }
    }

    @Override
    public List<AttendanceDate> selectAllUserAttendanceByDay(String date) {
        return dateMapper.selectAllUserAttendanceByDay(date);
    }

    @Override
    public List<AttendanceDate> findUserAttendanceByUserId(Map<String, Object> map) {
        return dateMapper.findUserAttendanceByUserId(map);
    }

    @Override
    public AttendanceStatis selectUserAttendanceStatis(Map<String, Object> mapToMapper) {
        return dateMapper.selectUserAttendanceStatis(mapToMapper);
    }
}
